package com.lyj.sc.tianchi;

import java.util.List;

/**
 * @program: code-random
 * @description:
 * @author: lyj
 * @create: 2022-07-14 11:19
 * @version: 1.0
 * @Modifier By:
 **/
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public class test2 {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode l1 = headA;
            ListNode l2 = headB;
            int m = 0;
            int n = 0;
            while (l1 != null) {
                l1 = l1.next;
                m++;
            }
            while (l2 != null) {
                l2 = l2.next;
                n++;
            }
            if (n == 0 || m == 0) {
                return null;
            }
            l1 = headA;
            l2 = headB;
            if (m > n) {
                while (n > 0) {
                    l1 = l1.next;
                }
            } else {
                while (m > 0) {
                    l2 = l2.next;
                }
            }
            while (l2 != null && l1 != null) {
                if (l1 == l2) {
                    return l1;
                }
                l1 = l1.next;
                l2 = l2.next;
            }
            return null;
        }
    }
}
